package indi.a709924470.emergencybutton;

import android.content.DialogInterface;
import android.didikee.donate.AlipayDonate;
import android.didikee.donate.WeiXinDonate;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class activity_settings extends AppCompatActivity {

    private Thread check;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_layout);

        Switch uai = (Switch) findViewById(R.id.iconchg);
        uai.setChecked(EmButton.useAppicon);
        uai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                EmButton.useAppicon = isChecked;
            }
        });

        ImageView ivap = (ImageView) findViewById(R.id.imageView2);
        ivap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                donateAli();
            }
        });

        ImageView ivwx = (ImageView) findViewById(R.id.imageView3);
        ivwx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                donateWechat();
            }
        });

        Button soc = (Button) findViewById(R.id.textView6);
        soc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert("源码许可","应用内捐赠 https://github.com/didikee/AndroidDonate\n" +
                        "recyclerview分割线 火龙裸先生(791335000@qq.com)");
            }
        });

        ((EditText) findViewById(R.id.ntitile)).setText(EmButton.nt == null ? "" : EmButton.nt.toString());
        ((EditText) findViewById(R.id.ncontent)).setText(EmButton.nc == null ? "" : EmButton.nc.toString());

        ((EditText) findViewById(R.id.ntitile)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                EmButton.nt = s;
            }
            @Override
            public void afterTextChanged(Editable s) {}
        });
        ((EditText) findViewById(R.id.ncontent)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                EmButton.nc = s;
            }
            @Override
            public void afterTextChanged(Editable s) {}
        });

        ((Button) findViewById(R.id.ntf)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EmButton.testNotify = EmButton.global_flag && !EmButton.testNotify;
            }
        });
        check = new Thread(new Runnable() {
            @Override
            public void run() {
                ((Button) findViewById(R.id.ntf)).setEnabled(EmButton.global_flag);
            }
        });
        check.start();
    }

    private void alert(String title , @NonNull String content, String pb, DialogInterface.OnClickListener dop, String nb, DialogInterface.OnClickListener don){
        AlertDialog.Builder adbl = new AlertDialog.Builder(this);
        adbl.setMessage(content==null?"":content);
        adbl.setTitle(title==null?"":title);
        if (pb == null || dop == null || nb == null || don == null){}else {
            adbl.setPositiveButton(pb, dop);
            adbl.setNegativeButton(nb, don);
        }
        AlertDialog ad = adbl.create();
        ad.show();
    }

    private void alert(String title , @NonNull String content){
        alert(title,content,null,null,null,null);
    }

    private void donateAli(){
        String payCode = "FKX01924LWVYGRWXQXV9AC";
        try{
            boolean hasInstalledAlipayClient = AlipayDonate.hasInstalledAlipayClient(this);
            if (hasInstalledAlipayClient) {
                AlipayDonate.startAlipayClient(this, payCode);
            }else{
                throw new NullPointerException();
            }
        }catch(Exception exp){
            AlertDialog.Builder ad = new AlertDialog.Builder(this);
            ad.setMessage("你还没安装支付宝呢~\n试试微信捐助？");
            ad.setTitle("哎呀...");
            ad.setPositiveButton("好的", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    donateWechat();
                    dialog.dismiss();
                }
            });
            ad.setNegativeButton("算了吧...", new DialogInterface.OnClickListener() {@Override
                public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }});
        }
    }

    private void donateWechat(){
        InputStream weixinQrIs = getResources().openRawResource(R.raw.wxdt);
        String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android" + File.separator +
                "weixindonate.png";
        WeiXinDonate.saveDonateQrImage2SDCard(qrPath, BitmapFactory.decodeStream(weixinQrIs));
        WeiXinDonate.donateViaWeiXin(this, qrPath);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(check != null){
            check.interrupt();
        }
    }
}
